<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>列出指定路径中的文件和目录</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.scandir" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">scandir</h1>
  <p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">scandir</span> &mdash; <span class="dc-title">列出指定路径中的文件和目录</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.scandir-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>scandir</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$directory</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$sorting_order</code></span>
   [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span>
  ]] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   返回一个 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>，包含有
   <code class="parameter">directory</code> 中的文件和目录。
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.scandir-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">directory</code></dt>

     <dd>

      <p class="para">
       要被浏览的目录
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">sorting_order</code></dt>

     <dd>

      <p class="para">
       默认的排序顺序是按字母升序排列。如果使用了可选参数
       <code class="parameter">sorting_order</code>（设为
       1），则排序顺序是按字母降序排列。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">context</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">context</code> 参数的说明见手册中的
       <a href="ref.stream.html" class="link">Streams API</a> 一章。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.scandir-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功则返回包含有文件名的 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>，如果失败则返回
   <strong><code>FALSE</code></strong>。如果 <code class="parameter">directory</code>
   不是个目录，则返回布尔值 <strong><code>FALSE</code></strong> 并生成一条
   <strong><code>E_WARNING</code></strong> 级的错误。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.scandir-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.4.0</td>
       <td>
        <code class="parameter">sorting_order</code> now accepts constants. Any
        nonzero value caused descending order in previous versions.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.scandir-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2754">
    <p><strong>Example #1 一个简单的 <span class="function"><strong>scandir()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dir&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'/tmp'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$files1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">scandir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$files2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">scandir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; .
    [1] =&gt; ..
    [2] =&gt; bar.php
    [3] =&gt; foo.txt
    [4] =&gt; somedir
)
Array
(
    [0] =&gt; somedir
    [1] =&gt; foo.txt
    [2] =&gt; bar.php
    [3] =&gt; ..
    [4] =&gt; .
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2755">
    <p><strong>Example #2 <span class="function"><strong>scandir()</strong></span> 在 PHP 4 中的实现</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dir&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/tmp"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dh&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">opendir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">!==&nbsp;(</span><span style="color: #0000BB">$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">readdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dh</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$files</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$filename</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">rsort</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; .
    [1] =&gt; ..
    [2] =&gt; bar.php
    [3] =&gt; foo.txt
    [4] =&gt; somedir
)
Array
(
    [0] =&gt; somedir
    [1] =&gt; foo.txt
    [2] =&gt; bar.php
    [3] =&gt; ..
    [4] =&gt; .
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.scandir-notes">
  <h3 class="title">注释</h3>
  <div class="tip"><strong class="tip">Tip</strong><p class="simpara">如已启用<a href="filesystem.configuration.html#ini.allow_url_fopen" class="link">fopen 包装器</a>，在此函数中， URL 可作为文件名。关于如何指定文件名详见 <span class="function"><a href="fopen.html" class="function">fopen()</a></span>。各种
wapper 的不同功能请参见 <a href="wrappers.html" class="xref">支持的协议和封装协议</a>，注意其用法及其可提供的预定义变量。</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.scandir-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="opendir.html" class="function" rel="rdfs-seeAlso">opendir()</a> - 打开目录句柄</span></li>
    <li class="member"><span class="function"><a href="readdir.html" class="function" rel="rdfs-seeAlso">readdir()</a> - 从目录句柄中读取条目</span></li>
    <li class="member"><span class="function"><a href="glob.html" class="function" rel="rdfs-seeAlso">glob()</a> - 寻找与模式匹配的文件路径</span></li>
    <li class="member"><span class="function"><a href="is_dir.html" class="function" rel="rdfs-seeAlso">is_dir()</a> - 判断给定文件名是否是一个目录</span></li>
    <li class="member"><span class="function"><a href="sort.html" class="function" rel="rdfs-seeAlso">sort()</a> - 对数组排序</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>